package com.sc.test.menutest;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import com.sc.test.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Shen Chao on 2018-03-13-013 上午 10:01.
 */

public class DragRvAdapter extends RecyclerView.Adapter<DragRvAdapter.ViewHolder> {
    private ArrayList<DataBean> mData;

    public DragRvAdapter(ArrayList<DataBean> mData) {
        this.mData = mData;
    }

    /**
     * 事件回调监听
     */
    private DragRvAdapter.OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(DragRvAdapter.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    /**
     * 添加条目
     */
    public void addNewItem(int pos) {
        if (mData == null) {
            mData = new ArrayList<>();
        }
        DataBean dataBean = new DataBean();
        dataBean.showClose = true;
        dataBean.text = "条目" + pos;
        mData.add(mData.size(), dataBean);
//        notifyItemInserted(pos);
        notifyDataSetChanged();
    }

    /**
     * 删除条目
     */
    public void deleteItem(int pos) {
        if (mData == null || mData.isEmpty()) {
            return;
        }
        mData.remove(pos - 1);
//        notifyItemRemoved(pos - 1);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_menu_test, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tv.setText(mData.get(position).text);

        if (mData.get(position).showClose) {
            holder.close.setVisibility(View.VISIBLE);
        } else {
            holder.close.setVisibility(View.GONE);
        }

        holder.close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, pos);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    showClose();
                    onItemClickListener.onItemLongClick(holder.itemView, pos);
                }
                //不再触发点击事件
                return true;
            }
        });


    }

    public void showClose() {
        for (DataBean mDatum : mData) {
            mDatum.showClose = true;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void move(int fromPosition, int toPosition) {
        DataBean s = mData.remove(fromPosition);
        mData.add(toPosition > fromPosition ? toPosition - 1 : toPosition, s);
        notifyItemMoved(fromPosition, toPosition);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_menu)
        TextView tv;
        @BindView(R.id.iv_close)
        ImageView close;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
